package com.csyaonie.android.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.csyaonie.android.R;

public class AFragment extends Fragment {

    private TextView fTv;
    private Button abt1;
    private Button abt2;
    private Button abt3;
    private SendMessage listener;

    public interface SendMessage{
        void onClick(String title);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            listener=(SendMessage) context;
        }catch (ClassCastException e){
            throw new ClassCastException("必须实现SendMessage");
        }
    }

    public static AFragment newInstance(String title) {
        Bundle args = new Bundle();
        AFragment fragment = new AFragment();
        args.putString("title",title);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle b=getArguments();
        fTv=view.findViewById(R.id.ftv1);
        fTv.setText("我是FragmentA"+b.getString("title"));
        abt1=view.findViewById(R.id.abt1);
        abt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fTv.setText("新文字");
            }
        });
        abt2=view.findViewById(R.id.abt2);
        abt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentActivity a= (FragmentActivity) getActivity();
                a.setData("FragmentA message");
            }
        });
        abt3=view.findViewById(R.id.abt3);
        abt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick("Interface message");
            }
        });
    }
}
